package com.wowangz.cms.systemmanage.headbank.api;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.wowangz.cms.common.pojo.AjaxDataEntity;
import com.wowangz.cms.common.utils.SysConstant;
import com.wowangz.cms.component.datagrid.pojo.DataGrid;
import com.wowangz.cms.component.datagrid.pojo.DataResult;
import com.wowangz.cms.component.datagrid.pojo.DataStyle;
import com.wowangz.cms.component.datagrid.pojo.PangingParam;
import com.wowangz.cms.component.datagrid.utils.DataTypeConstant;
import com.wowangz.cms.component.datagrid.utils.PangingTools;
import com.wowangz.cms.syscore.IssTransactionContainer;
import com.wowangz.cms.syscore.api.BaseController;
import com.wowangz.cms.syscore.api.JSONFactory;
import com.wowangz.cms.syscore.utils.MessageResourcesUtils;
import com.wowangz.cms.syscore.utils.ServletUtils;
import com.wowangz.cms.systemmanage.headbank.pojo.HeadBank;
import com.wowangz.cms.systemmanage.headbank.service.HeadBankService;

@Scope("prototype")
@Controller
public class HeadBankController extends BaseController{
	
	private static Logger logger = LoggerFactory.getLogger(HeadBankController.class);
	
	
	@RequestMapping(value="/systemmanage/headbank/queryHeadBankList.web")
	public String queryHeadBankList(HttpServletRequest request, HttpServletResponse response, Model model) 
	{
		return "/WEB-INF/jsp/systemmanage/headbank/headbankList";
	}
	
	
	
	@RequestMapping(value="/systemmanage/headbank/queryHeadBankListInfoDataGrid.json", method=RequestMethod.POST)
	public void queryHeadBankListInfoDataGrid(HttpServletRequest request, HttpServletResponse response, Model model)  
	{
		PangingParam pangingParam = null;
		DataResult  dataResult = new DataResult();
		DataGrid dataGrid = null;

		try 
		{
			pangingParam = (PangingParam) ServletUtils.getParametersToBaseEntity(request, PangingParam.class);
			HashMap<String, Object> map = (HashMap) ServletUtils.getParametersToHashMap(request);
			dataGrid = new DataGrid();
			dataGrid.setPangingParam(pangingParam);
			
			
			map.put("startrow",dataGrid.getStartrow() );
			map.put("endrow", dataGrid.getEndrow());
			map.put("rows", dataGrid.getRows());
			map.put("sort", dataGrid.getSort());
			map.put("order", dataGrid.getOrder());
			map.put("statusid", SysConstant.RecordStatus.VALID);
			
			HeadBankService headbankService = (HeadBankService)IssTransactionContainer.getInstance(HeadBankService.class,false);
			long resultCount = headbankService.findResultCount(map);
			List<HeadBank> resultList = headbankService.findResultList(map);
			
			
			DataStyle dataStyle = new DataStyle();
			dataStyle.setPageIndex(pangingParam.getPage());
			dataStyle.setDataCount(resultCount);
			dataStyle.setDataList(resultList);

			dataStyle.addDepict("id", DataTypeConstant.LONG);
			dataStyle.addDepict("banknum", DataTypeConstant.STRING);
			dataStyle.addDepict("bankname", DataTypeConstant.STRING);
			dataStyle.addDepict("directcode", DataTypeConstant.STRING);
			dataResult = PangingTools.buildResultPagerInfo(dataStyle);
			
			if(pangingParam.getReportExportType() == 0){
				response.getWriter().write(JSONFactory.toJSONString(dataResult, userContext));
			}
			
		}
		catch(Exception e){
			e.printStackTrace();
			userContext.setErrorMessage(MessageResourcesUtils.getMessage(request, "alertinfo.common.java.alert5"));
		}
	}
	
	
	@RequestMapping(value="/systemmanage/headbank/toAddHeadBank.web")
	public String toAddHeadBank(HttpServletRequest request, HttpServletResponse response, Model model)
	{
		return "/WEB-INF/jsp/systemmanage/headbank/headbankAdd";
	}
	
	
	@RequestMapping(value="/systemmanage/headbank/addHeadBank.web", method={RequestMethod.GET,RequestMethod.POST})
	public String addHeadBank(HttpServletRequest request, HttpServletResponse response, Model model)
	{
		HeadBank headbank = null;
		try
		{
			headbank = (HeadBank) ServletUtils.getParametersToBaseEntity(request, HeadBank.class);
			headbank.setInputtime(new Date());
			headbank.setModifytime(new Date());
			headbank.setStatusid(SysConstant.RecordStatus.VALID);
			HeadBankService headbankService = (HeadBankService)IssTransactionContainer.getInstance(HeadBankService.class,false);
			headbankService.add(headbank);
			userContext.setInfoMessage(MessageResourcesUtils.getMessage(request, "alertinfo.common.java.alert1"));
			userContext.setCloseWindow(true);
		}
		catch(Exception e)
		{
			e.printStackTrace();
			userContext.setErrorMessage(MessageResourcesUtils.getMessage(request, "alertinfo.common.java.alert2"));
		}
		return "/WEB-INF/jsp/systemmanage/headbank/headbankAdd";
	}
	
	
	@RequestMapping(value="/systemmanage/headbank/deleteHeadBank.json", method={RequestMethod.GET,RequestMethod.POST})
	public void deleteHeadBank(HttpServletRequest request, HttpServletResponse response, Model model)
	{
		AjaxDataEntity ajaxDataEntity = new AjaxDataEntity();
		try
		{
			Long headbankId = Long.valueOf(getRequest().getParameter("id"));
			HeadBank headbank = new HeadBank();
			headbank.setId(headbankId);
			headbank.setStatusid(SysConstant.RecordStatus.INVALID);
			HeadBankService headbankService = (HeadBankService)IssTransactionContainer.getInstance(HeadBankService.class,false);
			headbankService.update(headbank);
			userContext.setInfoMessage(MessageResourcesUtils.getMessage(request, "alertinfo.common.java.alert3"));
			response.getWriter().write(JSONFactory.toJSONString(ajaxDataEntity, userContext));
		}
		catch(Exception e)
		{
			e.printStackTrace();
			userContext.setErrorMessage(MessageResourcesUtils.getMessage(request, "alertinfo.common.java.alert4"));
		}
	}
	
	
	@RequestMapping(value="/systemmanage/headbank/toUpdateHeadBank.web")
	public String toUpdateHeadBank(HttpServletRequest request, HttpServletResponse response, Model model)
	{
		try
		{
			String headbankId = getRequest().getParameter("id");
			HeadBankService headbankService = (HeadBankService)IssTransactionContainer.getInstance(HeadBankService.class,false);
			HeadBank headbank = headbankService.findHeadBankById(headbankId);
			model.addAttribute("headbank", headbank);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return "/WEB-INF/jsp/systemmanage/headbank/headbankUpdate";
	}
	
	
	@RequestMapping(value="/systemmanage/headbank/updateHeadBank.web", method={RequestMethod.GET,RequestMethod.POST})
	public String updateHeadBank(HttpServletRequest request, HttpServletResponse response, Model model)
	{
		HeadBank headbank = null;
		try
		{
			headbank = (HeadBank) ServletUtils.getParametersToBaseEntity(request, HeadBank.class);
			headbank.setModifytime(new Date());
			HeadBankService headbankService = (HeadBankService)IssTransactionContainer.getInstance(HeadBankService.class,false);
			headbankService.update(headbank);
			userContext.setInfoMessage(MessageResourcesUtils.getMessage(request, "alertinfo.common.java.alert1"));
			userContext.setCloseWindow(true);
		}
		catch(Exception e)
		{
			e.printStackTrace();
			userContext.setErrorMessage(MessageResourcesUtils.getMessage(request, "alertinfo.common.java.alert2"));
		}
		return "/WEB-INF/jsp/systemmanage/headbank/headbankUpdate";
	}
	
	@RequestMapping(value = "/systemmanage/headbank/validattionHeadBank.json", method = { RequestMethod.GET, RequestMethod.POST })
	public void validattionHeadBank(HttpServletRequest request, HttpServletResponse response, Model model) {
		AjaxDataEntity ajaxDataEntity = new AjaxDataEntity();
		try {
			HeadBank headbank = null;
			StringBuffer errorMsg = new StringBuffer();
			HashMap<String, Object> map = new HashMap<String, Object>();
			headbank = (HeadBank) ServletUtils.getParametersToBaseEntity(request, HeadBank.class);
			
			HeadBankService headbankService = (HeadBankService) IssTransactionContainer.getInstance(HeadBankService.class, false);
			map.put("statusid", SysConstant.RecordStatus.VALID);
			if(null != headbank.getId()){
				map.put("id", headbank.getId());
			}
			map.put("banknum", headbank.getBanknum());
			if(!headbankService.validationHeadBank(map)){
				errorMsg.append(MessageResourcesUtils.getMessage(request, "systemmanage.headbank.add.bankcodeexist"));
			}
			map.remove("banknum");
			map.put("bankname", headbank.getBankname());
			if(!headbankService.validationHeadBank(map)){
				errorMsg.append(MessageResourcesUtils.getMessage(request, "systemmanage.headnban,add.banknameexist"));
			}
			if(StringUtils.isNotEmpty(errorMsg.toString())){
				userContext.setErrorMessage(errorMsg.toString());
			}
			response.getWriter().write(JSONFactory.toJSONString(ajaxDataEntity, userContext));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

